package com.xunk.scene.util;

import com.xunk.common.core.text.Convert;
import com.xunk.scene.enums.CompareOperatorEnum;
import org.springframework.util.ObjectUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.scene.util
 * @ClassName: ConditionUtil
 * @Author: luanzhiwei
 * @Description: 条件工具
 * @Date: 2021/5/12 14:18
 * @Version: 1.0
 */
public class ConditionUtil {

    /**
     * 条件大小比较
     *
     * @param current
     * @param target
     * @param compareOperator
     * @return
     */
    public static boolean compare(String current, String target, String compareOperator) {
        CompareOperatorEnum code = CompareOperatorEnum.getByCode(compareOperator);
        if (ObjectUtils.isEmpty(code)) {
            return false;
        }
        switch (code) {
            case GT:
                return Convert.toInt(current) > Convert.toInt(target);
            case LT:
                return Convert.toInt(current) < Convert.toInt(target);
            case BT:
                String[] split = target.split("-");
                return Convert.toInt(split[0]) < Convert.toInt(current) && Convert.toInt(split[1]) > Convert.toInt(current);
            case EQ:
                return Convert.toStr(current).equals(Convert.toStr(target));
            case GTEQ:
                return Convert.toInt(current) > Convert.toInt(target) || Convert.toInt(current).equals(Convert.toInt(target));
            case LTEQ:
                return Convert.toInt(current) < Convert.toInt(target) || Convert.toInt(current).equals(Convert.toInt(target));
            default:
                return false;
        }
    }

    /**
     * 判断是否周一到周五
     *
     * @return
     */
    public static boolean isWeekday() {
        return !isWeekend();
    }

    /**
     * 判断是否周末
     *
     * @return
     */
    public static boolean isWeekend() {
        LocalDate now = LocalDate.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        return DayOfWeek.SATURDAY.equals(dayOfWeek) || DayOfWeek.SATURDAY.equals(dayOfWeek);
    }

    /**
     * 判断当前是否在配置的循环周期内
     *
     * @param period
     * @return
     */
    public static boolean isSpecificWeekly(String period) {
        final int value = LocalDate.now().getDayOfWeek().getValue();
        return period.contains(Convert.toStr(value));
    }

}
